//
//  FswViewController.m
//  zhihuiguanjia
//
//  Created by apple on 16/4/3.
//  Copyright © 2016年 MyCompany. All rights reserved.
//

#import "FswViewController.h"

@interface FswViewController ()

@property (nonatomic, strong) NSMutableArray *ypList;
@property (nonatomic, strong) NSMutableArray *zulinList;
@property (nonatomic, strong) NSMutableArray *gouzhiList;
@property (nonatomic, strong) UIButton *back;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (retain, nonatomic) IBOutlet UITableView *tableView;

@end
@implementation FswViewController

- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    self.ypList = [NSMutableArray array];
    self.zulinList = [NSMutableArray array];
    self.gouzhiList = [NSMutableArray array];
    [super viewDidLoad];
    _tableView.frame  = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [[self view] addSubview:[self tableView]];
    self.networkManager = [[NetworkManager alloc] init];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self getCq];
    [self.view addSubview:_tableView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
}

- (void)getCq {
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           _zhuhuId,@"zhuhu_id", nil];
    [self.networkManager requestWithMethod:fushuwu parameter:param delegate:self];
    [self.HUD show:YES];
    
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    if (methodId == fushuwu) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            
            if([result isEqualToString:@"000"]){
                
                [self.gouzhiList   removeAllObjects];
                
                NSDictionary *results = [data objectForKeyOrNil:@"fushuwulist"];
                
                NSArray *gouzhiList  = results[@"gzList"];
                NSArray *zulinList  = results[@"zlList"];
               // NSArray *gouzhiList  =[results objectAtIndex:1];
                 if (![gouzhiList isKindOfClass:[NSNull class]] && [gouzhiList count] > 0) {
                     [self.gouzhiList addObjectsFromArray:gouzhiList] ;
                     
                 } else {
//                     UILabel *zxlabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,200,10)];
//                     zxlabel.text = @"暂无购置物信息";
//                     [self.view addSubview:zxlabel];
                     BTToast(@"暂无购置物信息");
                 }
                if (![zulinList isKindOfClass:[NSNull class]] && [zulinList count] > 0) {
                    [self.zulinList addObjectsFromArray:zulinList] ;
                    
                } else {
//                    UILabel *zxlabel = [[UILabel alloc] initWithFrame:CGRectMake(20,20,200,10)];
//                    zxlabel.text = @"暂无租赁物信息";
//                    [self.view addSubview:zxlabel];
                    BTToast(@"暂无租赁物信息");
                }
                [self.tableView reloadData];
            } else if([result isEqualToString:@"001"]){
                NSString * resultinfo = [data objectForKeyOrNil:@"resultinfo"];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:[resultinfo stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
                
            }
        }
    }
}

#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(section == 0){
        return [self.gouzhiList count];
    } else {
        return [self.zulinList count];
    }
    
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"获取失败，请稍后重试";
        }
    }
    
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0) {
        static NSString *GouzhiwuCell = @"GouzhiwuCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                 GouzhiwuCell];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:GouzhiwuCell];
        }
        if (self.gouzhiList != nil && self.gouzhiList.count >0) {
            NSUInteger row = [indexPath row];
            UILabel *fswNameLbl = (UILabel *)[cell.contentView viewWithTag:100];
            NSDictionary* dic = [self.gouzhiList objectAtIndex:row];
            
            NSString *fswNameStr = [[dic objectForKey:@"fswName"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
            fswNameLbl.text = fswNameStr;
            
        }
        return cell;

    } else {
        static NSString *GouzhiwuCell = @"GouzhiwuCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                                 GouzhiwuCell];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc]
                    initWithStyle:UITableViewCellStyleDefault
                    reuseIdentifier:GouzhiwuCell];
        }
        if (self.zulinList != nil && self.zulinList.count >0) {
            NSUInteger row = [indexPath row];
            UILabel *fswNameLbl = (UILabel *)[cell.contentView viewWithTag:100];
            NSDictionary* dic = [self.zulinList objectAtIndex:row];
            
            NSString *fswNameStr = [[dic objectForKey:@"fswName"] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            
            fswNameLbl.text = fswNameStr;
            
        }
        return cell;

    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *myHeader = [[UIView alloc] init];
    UILabel *myLabel = [[UILabel alloc] init];
    myLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:14.0];
    UIImageView *myImage = [[UIImageView alloc ] initWithFrame:CGRectMake(0, 0, 120, 25)];
    [myImage setImage:[UIImage imageNamed:@"biaoshi.png"]];
    myLabel.textColor = [UIColor whiteColor];
   
    switch (section) {
        case 0:
            [myLabel setFrame:CGRectMake(40, 10, 60, 10)];
            [myLabel setTag:100];
            [myLabel setBackgroundColor:[UIColor clearColor]];
            [myLabel setText:@"购置物"];
            //[myHeader setBackgroundColor:[UIColor clearColor]];
            break;
        case 1:
            [myLabel setFrame:CGRectMake(40, 10, 60, 10)];
            [myLabel setTag:101];
            [myLabel setBackgroundColor:[UIColor clearColor]];
            [myLabel setText:@"租赁物"];

            //[myHeader setBackgroundColor:[UIColor clearColor]];
            break;
        default:
            break;
    }
    
    
    [myHeader addSubview:myImage];
    [myHeader addSubview:myLabel];
    
    return myHeader;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}


@end
